package com.tom.controller;

import com.tom.pojo.FoodCate;
import com.tom.service.ICateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cate")
public class CateController {
    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    private ICateService cateService;

    /**
     * 获取餐品类别列表
     * @param rid   店铺id
     * @return  获取结果
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> cateList (@RequestParam Integer rid) {
        responseMsg.clear();
        List<FoodCate> list = cateService.listCate(rid);
        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "list cate success");
            responseMsg.put("cate_list", list);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "list cate failure");
        }
        return responseMsg;
    }

    /**
     * 新增餐品类别
     * @param cate 餐品类别对象
     * @return  新增结果
     */
    @RequestMapping("/newcate")
    @ResponseBody
    public Map<String, Object> newAddress(@RequestBody FoodCate cate) {
        responseMsg.clear();
        int result = cateService.newCate(cate);
//        System.out.println(cate.getAddress());
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "add new cate success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "add new cate failure");
        }
        return responseMsg;
    }

    /**
     * 更新餐品类别
     * @param cate 餐品类别对象
     * @return  更新结果
     */
    @RequestMapping("/updatecate")
    @ResponseBody
    public Map<String, Object> updateAddress(@RequestBody FoodCate cate) {
        responseMsg.clear();

        int result = cateService.updateCate(cate);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "update cate success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "update cate failure");
        }
        return responseMsg;
    }

    /**
     * 删除餐品类别
     * @param id 地址id
     * @return  删除结果
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteAddress(@RequestParam Integer id) {
        responseMsg.clear();
        int result = cateService.deleteCate(id);
        if (result>0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "delete cate success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "delete cate failure");
        }
        return responseMsg;
    }
}
